package com.oa.demo.gatewary.configure;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * 跨域设置
 * <p>
 * Spirng Cloud Gateway不是基于Servlet，而是基于Reactive编程模型的WebFlux。
 *
 * @author 孔德成
 * @date 2020/11/23 13:51
 */
@Configuration
public class OaGateWayCorsConfigure {

    @Bean
    public CorsWebFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        CorsConfiguration cors = new CorsConfiguration();
        cors.setAllowCredentials(true);//表示允许cookie跨域；
        cors.addAllowedOrigin(CorsConfiguration.ALL);//表示允许任何来源
        cors.addAllowedHeader(CorsConfiguration.ALL);//表示请求头部允许携带任何内容；
        cors.addAllowedMethod(CorsConfiguration.ALL); //表示允许任何HTTP方法。权限关联
        source.registerCorsConfiguration("/**", cors);
        return new CorsWebFilter(source);
    }

}
